<template>
  <div class="bread-crumb">
    <el-card class="bread-content">
      <slot></slot>
      <i class="el-icon-position"></i>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }" v-show="!isIndex">
          <span @click="handleClick">首页</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-for="item in breadList" :key="item.path">
          {{ item.meta.title }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      breadList: []
    }
  },

  watch: {
    $route(route) {
      let filterList = route.matched.filter(item => {
        if (item.meta && item.meta.title) {
          return item
        }
      })
      this.breadList = filterList
    }
  },

  computed: {
    // 判断是否在首页
    isIndex() {
      let isIndexPage = false
      this.breadList.forEach(item => {
        if (item.meta.groupId === -1) {
          isIndexPage = true
        } else {
          isIndexPage = false
        }
      })
      return isIndexPage
    }
  },

  created() {
    let filterList = this.$route.matched.filter(item => {
      if (item.meta && item.meta.title) {
        return item
      }
    })
    this.breadList = filterList
  },

  methods: {
    handleClick() {
      this.$bus.$emit('tabchange', -1)
    }
  }
}
</script>

<style lang="scss" scoped></style>
